package com.sdkj.dspservice.modules.sys.service.impl;

import com.sdkj.dspservice.common.base.service.impl.CrudServiceImpl;
import com.sdkj.dspservice.common.utils.StringUtils;
import com.sdkj.dspservice.modules.sys.dao.DictDao;
import com.sdkj.dspservice.modules.sys.entity.Dict;
import com.sdkj.dspservice.modules.sys.service.DictService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 字典表service实现类
 *
 * @author tm
 * @version 2021/4/21 18:09
 */
@Service
@Transactional(readOnly = true)
public class DictServiceImpl extends CrudServiceImpl<DictDao, Dict> implements DictService {
    @Override
    public boolean saveOrUpdate(Dict dict) {
        boolean result = true;
        // 保存时，支持“,”一次性保存多个
        if(dict.isNewRecord()){
            if(StringUtils.isNotBlank(dict.getLabel()) && StringUtils.isNotBlank(dict.getValue())){
                String[] labels = dict.getLabel().split(",");
                String[] values = dict.getValue().split(",");
                if(labels.length == values.length){
                    for(int i = 0; i<labels.length; i++){
                        Dict dictSave = new Dict();
                        dictSave.setType(dict.getType());
                        dictSave.setDescription(dict.getDescription());
                        dictSave.setLabel(labels[i]);
                        dictSave.setValue(values[i]);
                        result = super.saveOrUpdate(dictSave);
                    }
                }else {
                    result = super.saveOrUpdate(dict);
                }
            }else {
                result = super.saveOrUpdate(dict);
            }
        }else {
            result = super.saveOrUpdate(dict);
        }
        return result;
    }
}
